//
//  SCNRankingViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/4/25.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNRankingViewController: SCNTableViewController {

    var investData: [SCNRankingInvestListModels]?
    var integralData: [SCNRankingIntegralListModels]?
    
    var headerIndex = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "财富排行"
        self.createTableView(self.view.frame, style: .grouped, isRefreshNewData: false, isRefreshMoreData: false) { (tableView) in
            tableView.register(SCNRankingTableViewCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(SCNRankingTableViewCell.classForCoder()))
        }
        request()

    }

    override func request() {

        super.request()

        
        SCNRankingInvestViewModel.requestOtherInvest { (result) in
            self.investData = result
            self.tableView.reloadData()
        }

        SCNRankingIntegralViewModel.requestOtherIntegral { (result) in
            self.integralData = result
            self.tableView.reloadData()
        }
    }
    

}

extension SCNRankingViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let tmp = self.investData {
            return tmp.count
        }else {
            return 0
        }
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(SCNRankingTableViewCell.classForCoder()), for: indexPath) as! SCNRankingTableViewCell
        
        
        if self.headerIndex == 0, let tmpInves = investData {
            let obj = tmpInves[indexPath.row]
            cell.titleL.text =  obj.mobile
            cell.detailL.text = obj.account
            
        }
        if self.headerIndex == 1, let tmpIntegral = integralData {
            let obj = tmpIntegral[indexPath.row]
            cell.titleL.text =  obj.mobile
            cell.detailL.text = obj.integral
            
        }
        cell.index = indexPath.row
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = SCNRankingHeaderView(frame: CGRect(x: 0,y: 0,width: SCNConstants.SCREENWIDTH,height: 130))
        
        headerView.segmentedIndex = self.headerIndex
        
        headerView.segmentedActionBlock = { [weak self] index in
            self?.headerIndex = index
            self?.tableView.reloadData()
        }
        
        return headerView
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return self.headerIndex == 0 ? 100 : 130
    }
}
